//
//  AnimateCircleLayer.m
//  Footer
//
//  Created by leotao on 16/12/27.
//  Copyright © 2016年 leotao. All rights reserved.
//

#import "AnimateCircleLayer.h"

#define kCenter_Gap  40
// 30度角
#define kDegree      M_PI / 6.0
#define kLineW       6
#define kArrowLength 6.0

@implementation AnimateCircleLayer

- (void)drawInContext:(CGContextRef)ctx
{
    [super drawInContext:ctx];
    
    CGPoint pointA;
    CGPoint pointB;
    CGFloat pointbX = kCenter_Gap;
    CGFloat pointaX = pointbX;
    CGPoint pointC;
    CGPoint pointD;
    CGFloat pointcX = self.bounds.size.width - kCenter_Gap;
    CGFloat pointdX = pointcX;
    CGFloat radius = (self.bounds.size.width - 2 * kCenter_Gap) * 0.5;
    CGFloat H = self.bounds.size.height;
    CGFloat lineH = H * 0.3;
    
    CGFloat pointbY;
    CGFloat pointaY;
    CGFloat pointcY;
    CGFloat pointdY;
    
    UIBezierPath *bottomArcPath = nil;
    UIBezierPath *topArcPath = nil;
    UIBezierPath *bottomArrowPath = [UIBezierPath bezierPath];
    UIBezierPath *topArrowPath = [UIBezierPath bezierPath];
    
    if (self.progress < 0) self.progress = 0;
    if (self.progress > 1) self.progress = 1;
    
    // 当前弧线到达的角度 其中M_PI * 0.9是最大角度 防止上下两条曲线闭合 不闭合好看很多
    CGFloat currentAngle = (self.progress - 0.5) * 2 * (M_PI * 0.9);

    
    if (self.progress <= 0.5) {
        pointbY = (2 * lineH - H) * self.progress + H - lineH;
        pointaY = (2 * lineH - H) * self.progress + H;
        pointcY = (H - 2 * lineH) * self.progress;
        pointdY = (H - 2 * lineH) * self.progress + lineH;
    } else {    // A点继续向H / 2中移动 B点保持不动 同时画圆弧 同理D点不动C点动
        pointbY = H * 0.5;
        pointaY = (2 * lineH) * (1 - self.progress) + H * 0.5;
        pointdY = pointbY;
        pointcY = (2 * lineH) * (self.progress - 1) + H * 0.5;
        
        CGPoint center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
        // 最大弧度为0.9π 保证两条曲线不闭合
        bottomArcPath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:M_PI endAngle:M_PI + currentAngle  clockwise:YES];
        topArcPath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle: currentAngle clockwise:YES];
    }
    
    pointB = CGPointMake(pointbX, pointbY);
    pointA = CGPointMake(pointaX, pointaY);
    pointC = CGPointMake(pointcX, pointcY);
    pointD = CGPointMake(pointdX, pointdY);
    
    UIBezierPath *bottomLinePath = [UIBezierPath bezierPath];
    [bottomLinePath moveToPoint:pointA];
    [bottomLinePath addLineToPoint:pointB];
    
    UIBezierPath *topLinePath = [UIBezierPath bezierPath];
    [topLinePath moveToPoint:pointC];
    [topLinePath addLineToPoint:pointD];
    
    UIBezierPath *appendPath = [UIBezierPath bezierPath];
    if (bottomArcPath) {
        [appendPath appendPath:bottomArcPath];
        [bottomArrowPath moveToPoint:bottomArcPath.currentPoint];
        [bottomArrowPath addLineToPoint:[self arrowLinePointWithCurve:bottomArcPath.currentPoint bottomPath:YES currentAngle:currentAngle]];
    } else {
        CGPoint bottomArrowPoint = [self arrowLinePointWithPoint:pointB bottomPath:YES];
        [bottomArrowPath moveToPoint:pointB];
        [bottomArrowPath addLineToPoint:bottomArrowPoint];
    }
    
    if (topArcPath) {
        [appendPath appendPath:topArcPath];
        [topArrowPath moveToPoint:topArcPath.currentPoint];
        [topArrowPath addLineToPoint:[self arrowLinePointWithCurve:topArcPath.currentPoint bottomPath:NO currentAngle:currentAngle]];
    } else {
        CGPoint topArrowPoint = [self arrowLinePointWithPoint:pointD bottomPath:NO];
        [bottomArrowPath moveToPoint:pointD];
        [bottomArrowPath addLineToPoint:topArrowPoint];
    }

    [bottomLinePath appendPath:bottomArrowPath];
    [topLinePath appendPath:topArrowPath];
    [appendPath appendPath:bottomLinePath];
    [appendPath appendPath:topLinePath];
    
    //    NSLog(@"%@", NSStringFromCGRect(self.bounds));
    CGContextSetStrokeColorWithColor(ctx, [UIColor blackColor].CGColor);
    CGContextSetLineWidth(ctx, kLineW / self.contentsScale);
    CGContextAddPath(ctx, appendPath.CGPath);
    CGContextStrokePath(ctx);
}

/**
 画直线的箭头
 @param point 箭头起点
 @param bottom 是否是底部出来的线或者是顶部出来的线
 */
- (CGPoint)arrowLinePointWithPoint: (CGPoint)point bottomPath: (BOOL)bottom
{
    // 直线很好算。减去相应的临边和对边就行
    CGPoint arrowPoint = bottom ? CGPointMake(point.x - kArrowLength * sinf(kDegree), point.y + kArrowLength * cosf(kDegree)) : CGPointMake(point.x + kArrowLength * sinf(kDegree), point.y - kArrowLength * cosf(kDegree));
    
    return arrowPoint;
}


/**
 画曲线的箭头

 @param point 箭头的起点
 @param bottom 是否是底部出来的曲线或者是顶部出来的曲线
 @param currenAngle 曲线当前的角度
 */
- (CGPoint)arrowLinePointWithCurve: (CGPoint)point bottomPath: (BOOL)bottom currentAngle: (CGFloat)currenAngle
{
    // 转换为另外一个角 直角三角形用90度减
    CGFloat caculateDegree = M_PI_2 - kDegree;
    // 实际的计算角度是默认的角度减去当前旋转的角度 我也不懂为什么这么做
    CGFloat actualAngle = caculateDegree - currenAngle;
    
    CGPoint arrowPoint = bottom ? CGPointMake(point.x - kArrowLength * cosf(actualAngle), point.y + kArrowLength * sinf(actualAngle)) : CGPointMake(point.x + kArrowLength * cosf(actualAngle), point.y - kArrowLength * sinf(actualAngle));
    
    return arrowPoint;
}

@end
